package activity.personal;

import android.Manifest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import base.BaseActivity;
import base.MyApplcation;
import logincontroller.LoginController;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.SPUtils;
import util.utls.XImageLoader;
import widget.RoundImageView;

/**
 * 个人中心
 */

public class ActivityPerSonal extends BaseActivity {
    private TextView tvTitle, invate_tv;
    //    private  TextView tvTest;
    private ImageView back, imgTest;
    private RoundImageView person_img;
    private TextView weixiu;
    private TextView user_name,user_num,my_wallet,address;

    public static String NICHENG = "nicheng";// 修成昵称成功之后的广播
    RelativeLayout top;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_person_data);
        setStatusBarColor(R.color.main_color);
//        tvTest.setBackground(getResources().getDrawable(R.drawable.shezhi));
        tvTitle.setText("个人中心");
        tvTitle.setTextSize(20);
        tvTitle.setTextColor(getResources().getColor(R.color.white));
        //昵称更改的广播
        IntentFilter filter1 = new IntentFilter("chaingLogin");
        registerReceiver(BroadcastReceiver, filter1);


        //昵称更改的广播
        IntentFilter heada = new IntentFilter("filter1");
        registerReceiver(AddreHead, heada);
        if (IsUtilUid.isHead().equals("")) {
            person_img.setBackgroundResource(R.mipmap.default_head);
        }else {
            XImageLoader.load(Const.IMAGE_HEAD + IsUtilUid.isHead(),
                    person_img);
        }
        String uid = (String) SPUtils.get(MyApplcation.ctx, null, Const.RELE, "");
        Log.d("grupID",uid);
        read();
//        //退出登录的广播
        IntentFilter intentFilterExit = new IntentFilter(
               "EXITCHANGE");
       MyApplcation.ctx
                .registerReceiver(nichengBroadcastReceiver, intentFilterExit);
        isCameraPermission(this,1);


    }
    //6.0添加拍照的权限
    private static String[] PERMISSIONS_CAMERA_AND_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA};
    public static boolean isCameraPermission(Activity context, int requestCode){
        if (Build.VERSION.SDK_INT >= 23) {
            int storagePermission = ActivityCompat.checkSelfPermission(context,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE);
            int cameraPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
            if (storagePermission != PackageManager.PERMISSION_GRANTED || cameraPermission!= PackageManager.PERMISSION_GRANTED ) {
                ActivityCompat.requestPermissions(context, PERMISSIONS_CAMERA_AND_STORAGE,
                        requestCode);
                return false;
            }
        }
        return true;
    }

    BroadcastReceiver AddreHead = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {

            XImageLoader.load(Const.IMAGE_HEAD + IsUtilUid.isHead(),
                    person_img);
            read();
            Log.d("zhixinglema ",IsUtilUid.isHead());
        };
    };

    BroadcastReceiver BroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            XImageLoader.load(Const.IMAGE_HEAD + IsUtilUid.isHead(),
                    person_img);
            Log.d("zhixinglema ",IsUtilUid.isHead());
            read();
        };
    };


    BroadcastReceiver nichengBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String nikeName = (String) SPUtils.get(ActivityPerSonal.this, null,
                    Const.NICK, "");
            user_name.setText("用户名：" + nikeName);
            XImageLoader.load(Const.IMAGE_HEAD + IsUtilUid.isHead(),
                    person_img);
            read();
        };
    };



    /**
     * 加入数据
     */
    private void read() {
        String nikeName = (String) SPUtils.get(MyApplcation.ctx, null,
                Const.NICK, "");
        user_name.setText("用户名:" + nikeName);
        String mobile = (String) SPUtils.get(MyApplcation.ctx, null,
                Const.MOBILE, "");
        user_num.setText(mobile);
        String head = (String) SPUtils.get(MyApplcation.ctx, null, Const.HEAD, "");
        if (head != null) {
            XImageLoader.load(Const.IMAGE_HEAD + IsUtilUid.isHead(),
                    person_img);
        } else {
            person_img.setImageResource(R.drawable.users);
        }

    }


    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    /**
     * 退出登录
     */
    private void deleteInfo() {
        SPUtils.put(this, null, Const.ID, "");
        SPUtils.put(this, null, Const.USERNAME, "");
        SPUtils.put(this, null, Const.PASSWORD, "");
        SPUtils.put(this, null, Const.MOBILE, "");
        SPUtils.put(this, null, Const.IS_LOGINED, false);
        SPUtils.put(this, null, Const.HEAD, "");
        SPUtils.put(this, null, Const.NICK, "");
        SPUtils.put(this, null, Const.RELE, "");
        SPUtils.put(this, null, Const.PaySecret, "");
    }

    @Override
    public void onClick(View v) {
        Intent login=null;
        switch (v.getId()) {
            case R.id.back:
                this.finish();
                break;
            case R.id.person_img:
                //个人信息
                LoginController.PersonnalInfomaton(ActivityPerSonal.this,null);
                break;
            case R.id.top:
                LoginController.PersonnalInfomaton(ActivityPerSonal.this,null);
                break;
            case R.id.weixiu:
                //维修

                LoginController.onWeiXiu(ActivityPerSonal.this,null);
//                login = new Intent(ActivityPerSonal.this, WeiXiuRecord.class);
//                startActivity(login);
                break;
            case R.id.address:
                LoginController.onJineng(ActivityPerSonal.this,null);
//                login = new Intent(ActivityPerSonal.this, ActivityRepair.class);
//                startActivity(login);
                break;
            case R.id.imgTest:
                login = new Intent(ActivityPerSonal.this, ActivitySetUp.class);
                startActivity(login);
                break;
            case R.id.invate_tv:
//                login = new Intent(ActivityPerSonal.this, ActivityLogin.class);
//                startActivity(login);
                break;
            case R.id.my_wallet:
                LoginController.onQian(ActivityPerSonal.this,null);
//                login = new Intent(ActivityPerSonal.this, ActivityQianBao.class);
//                startActivity(login);
                break;



        }
    }
}
